﻿using System.Collections.Generic;
using System.Windows.Forms;
using Core.ProcessManager;
using Core.ProcessManager.Messenger;
using Core.ProcessManager.ProcessLoader;
using Infrastructure.IocContainer;

namespace View.ConsoleViewer
{
    public partial class MainForm : Form
    {
        private List<ListViewAction> _listViewActions;
        private readonly ProcessPerformer _processPerformer;

        public MainForm()
        {
            InitializeComponent();

            var container = ContainersFactory.GetContainer();
            var messenger = container.Resolve<IMessenger>();
            var processLoader = container.Resolve<IProcessLoader>();
            _processPerformer = new ProcessPerformer(messenger, processLoader);

            _processPerformer.LoadProcesses();
            PrepareListView(_processPerformer);

            messenger.OnMessengerStateChange += MessengerStateChanged;
        }

        void MessengerStateChanged(IMessenger sender, MessengerEventsArgs args)
        {
            if (listView.InvokeRequired)
            {
                listView.Invoke(new MessengerEventHandler(MessengerStateChanged), new object[]{sender, args});
            }
            else
            {
                textBox.Text += args.ActionName + " \n";
                foreach (ListViewItem item in listView.Items)
                {
                    if (item.Text == args.ActionName)
                    {
                        item.SubItems[0].Text = args.ActionState;
                        item.SubItems[1].Text = string.Format("{0}%", args.ActionProgress);
                    }
                }
            }
        }

        private void PrepareListView(ProcessPerformer processPerformer)
        {
            _listViewActions = new List<ListViewAction>();

            CreateGroups(processPerformer);
            CreateColumns();
            FillListView();
        }

        private void CreateGroups(ProcessPerformer processPerformer)
        {
            if (processPerformer.IsValid)
            {
                var processes = processPerformer.Processes;
                foreach (var process in processes)
                {
                    listView.Groups.Add(new ListViewGroup(process.Name, process.Name));
                    foreach (var action in process.Actions)
                    {
                        _listViewActions.Add(
                            new ListViewAction(action.Name));
                    }
                }
            }
        }

        private void FillListView()
        {
            for (var i = 0; i < _listViewActions.Count; i++)
            {
                var listViewAction = _listViewActions[i];

                var item = new ListViewItem
                               {
                                   Group = listView.Groups[listViewAction.Group],
                                   Text = listViewAction.Name
                               };

                item.SubItems.Add(listViewAction.State);
                item.SubItems.Add(string.Format("{0}%", listViewAction.Progress));

                listView.Items.Add(item);
            }
        }

        private void CreateColumns()
        {
            listView.View = System.Windows.Forms.View.Details;
            listView.Columns.Add("Name", 250);
            listView.Columns.Add("State", 300);
            listView.Columns.Add("Progress", 70);
            listView.Items.Clear();
        }

        private void ToolStripButtonClick(object sender, System.EventArgs e)
        {
            if(_processPerformer.IsValid)
                _processPerformer.ExecuteProcesses();
        }
    }
}